#!/bin/bash

echo "🔧 解决ARM64架构的node-sass问题..."

# 创建ARM64兼容的Dockerfile
cat > Dockerfile.arm64-fix << 'EOF'
# 使用x86_64架构的Node.js镜像来解决ARM64兼容性问题
FROM --platform=linux/amd64 node:14.21.3-alpine

WORKDIR /app

# 安装系统依赖
RUN apk add --no-cache \
    python3 \
    make \
    g++ \
    git \
    libc6-compat \
    linux-headers

# 设置环境变量
ENV PYTHON=/usr/bin/python3
ENV PYTHONPATH=/usr/bin/python3
ENV SASS_BINARY_SITE=https://npmmirror.com/mirrors/node-sass/
ENV NODE_SASS_SKIP_SASS_BINARY_DOWNLOAD_FOR_CI=1
ENV NODE_SASS_FORCE_BINARY=1

# 配置npm
RUN npm config set registry https://registry.npmmirror.com/
RUN npm config set sass_binary_site https://npmmirror.com/mirrors/node-sass/

# 创建.npmrc
RUN echo "registry=https://registry.npmmirror.com/" > .npmrc
RUN echo "sass_binary_site=https://npmmirror.com/mirrors/node-sass/" >> .npmrc

# 复制package.json
COPY package.json ./

# 强制重新构建node-sass二进制文件
RUN npm install --build-from-source --unsafe-perm --legacy-peer-deps --force --ignore-optional --ignore-engines

# 验证node-sass
RUN node -e "console.log('node-sass版本:', require('node-sass').info)"

# 构建测试
RUN npm run build:mp-weixin

# 检查输出
RUN echo "检查构建输出:"
RUN ls -la dist/build/mp-weixin/

CMD ["tail", "-f", "/dev/null"]
EOF

echo "✅ 创建了ARM64兼容的Dockerfile"
echo ""
echo "🔧 现在可以构建ARM64兼容的镜像:"
echo "docker build --platform linux/amd64 -f Dockerfile.arm64-fix -t test-arm64-fix ."
echo ""
echo "或者使用Docker的模拟功能:"
echo "docker run --platform linux/amd64 -it --rm test-arm64-fix /bin/sh"
